package Servlet;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;

/**
 * @Author Su
 * @Date 2025/4/30 16:11
 * @Version 1.0 （版本号）
 */

//获取文件统一接口
@WebServlet("/api/file/*")
public class FileServlet extends HttpServlet {
    private final String UPLOAD_DIR = "D:/su的项目/feishu_project/uploads/";

    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws IOException {
        String filePath = UPLOAD_DIR + req.getPathInfo(); // 如 /avatar/xxx.png
        File file = new File(filePath);

        if (file.exists()) {
            resp.setContentType(getServletContext().getMimeType(file.getName()));
            Files.copy(file.toPath(), resp.getOutputStream());
        } else {
            resp.sendError(404);
        }
    }
}